cc.Class({
    extends: cc.Component,

    properties: {

    },

    connect:function(url , options){
        let self = this ;
        this.ws = new WebSocket(url);
        this.ws.onopen = function (event) {
            console.log("Send Text WS was opened.");
        };
        this.ws.onmessage = function (event) {
            var data = self.parse(event.data) ;
            if(data!=null && data.event != null){
                cc.beimi.event[data.event](event.data);
            }
            console.log("response text msg: " + event.data);
        };
        this.ws.onerror = function (event) {
            console.log("Send Text fired an error");
        };
        this.ws.onclose = function (event) {
            console.log("WebSocket instance closed.");
        };
        return this;
    },
    on:function(command , func){
        cc.beimi.event[command] =  func ;
    },
    exec:function(command , data){
        console.log('this.ws.readyState',this.ws.readyState);
        if (this.ws.readyState === WebSocket.OPEN) {
            data.command = command;
            // data.userid = cc.beimi.user.id ;
            // data.orgi = cc.beimi.gameData.orgi ;
            // self.io = require("IOUtils");
            // let token = self.io.get("userToken");
            // data.token = token ;
            let sendData = JSON.stringify(data);
            console.log('send data is ',sendData);
            this.ws.send(sendData);
        }
    },
    emit:function(command , data){
        let param = {
            data : data
        } ;
        this.exec(command , param) ;
    },
    disconnect:function(){

    },
    parse:function(result){
        return JSON.parse(result) ;
    },
});
